<!--
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-09-02 15:56:28
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-09-18 09:44:35
 * @FilePath: \linePatrol\vue-element-plus-admin\src\views\SecurCheck\LedgerMessage\UserLedger\components\UserLedger.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { UserLedgerListType } from '@/api/securcheck/type'
import { useSecurCheckStore } from '@/store/modules/securcheck'
const { required } = useValidator()
const props = defineProps({
  tbUserLedger: {
    type: Object as PropType<Nullable<UserLedgerListType>>,
    default: () => null
  },
  formSchema: {
    type: Array as PropType<FormSchema[]>,
    default: () => []
  },
  FormDisabled: {
    type: Boolean as PropType<boolean>,
    default: () => null
  }
})
const SecurCheckStore = useSecurCheckStore()
// console.log(props.currentRow);
// props.currentRow?.map(item=>{
// })
const rules = reactive({
  meterState: [required()],
  meterType: [required()],
  devName: [required()],
  deptId: [required()],
  poDate: [required()],
  isRed: [required()],
  isCheck: [required()],
  areaId: [required()]
})
const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    formData.file = formData.file.map((item: any) => {
      if (item.response) {
        return item.response.data
      } else {
        return item
      }
    })
    formData.file = JSON.stringify(formData.file)
    formData.userLedgerUuid = formData.tbUserLedger.uuid
    return formData
  }
}
watch(
  () => props.tbUserLedger,
  async (currentRow) => {
    if (!currentRow) return
    const formData = await getFormData()
    formData.tbUserLedger = currentRow
    SecurCheckStore.setMeterLedger(formData)
  }
)
watch(
  () => SecurCheckStore.MeterLedgers,
  async (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)
defineExpose({
  submit
})
</script>

<template>
  <Form
    :disabled="props.FormDisabled"
    :rules="rules"
    @register="formRegister"
    :schema="formSchema"
  />
</template>
<style scoped>
/* :deep(.el-col) {
  padding: 10px 15px 5px 0px !important;
}
#4099f5 */
:deep(.el-divider--horizontal) {
  border-top: 1px #4099f5 solid !important;
}
:deep(.el-divider__text) {
  color: #4099f5;
}
</style>
